<template>
  <div class="header">
    <h2>TodoList</h2>
    <div class="input">
      <input type="text" placeholder="请输入待办事项....." v-model="thing" @keyup.enter="addTodo"/>
      <button @click="addTodo">添加</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import {v4 as uuidv4} from 'uuid';
const thing = ref("");
const emits = defineEmits(['sendTodo'])
//向App组件传递一个todo的对象
const addTodo = ()=>{
  const value = thing.value.trim();
  if(value){
    const todo = {
      id:uuidv4(),
      content:thing.value.trim(),
      done:false,
    }
    emits('sendTodo',todo);
    thing.value = "";
  }else{
    confirm("请输入有效的内容，内容不能为空");
  }
}
</script>

<style scoped>
.header {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 10px 0px;
  width: 100%;
  background-color: #0b87ff;
}
.header h2 {
  margin-bottom: 10px;
  color: white;
}
.header .input input {
  padding: 5px 20px;
  margin-right: 10px;
  border-radius: 5px;
  width: 400px;
  height: 30px;
  border: none;
  outline: none;
}
.header .input button {
  width: 80px;
  height: 30px;
  color: #0b87ff;
  border-radius: 5px;
  border: none;
  outline: none;
  vertical-align: middle;
}
</style>